﻿using Autofac;
using Autofac.Core;
using Common;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Models.PrintRecord;
using ZoneLine.Stations;

namespace ZoneLine.Printers
{
    /// <summary>
    /// 理想汽车打印帮助
    /// </summary>
    public class LXQCPrinter : IPrinter
    {
        public string PrintLabel(string printTemplatePath, string printerName, int startIndex, int? endIndex = null)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();

            var (paras, code) = GetPara(startIndex);

            PrinterHelper.PrinterLabel(printTemplatePath, printerName, paras);
            return code;
        }

        public void PrintLabelShow(string printTemplatePath, string printerName, int startIndex, int? endIndex = null)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();

            var (paras, code) = GetPara(startIndex);

            PrinterHelper.PrinterLabelShow(printTemplatePath, printerName, paras);
        }


        private (Dictionary<string, string>, string) GetPara(int index)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var lxConfig = config.LxqcConfig;
            var paras = new Dictionary<string, string>();
            var para = StationEnableConfig.GetPara();

            //供应商代码，固定
            paras.Add("GYSDM_N1", lxConfig.SupplyNo);
            //零部件序列号 固定
            var serialNo = GetPartSerialNo(index);
            //paras.Add("LBJXLH_N1", serialNo);

            //生产批次
            var batchNo = GetProductBatch();
            paras.Add("LBJXLH_B_N1", batchNo + "/" + serialNo);
            paras.Add("BBH_N1", lxConfig.Version);
            var partNo = string.Empty;
            if (Cache.WorkDirection == WorkDirectionEnum.Left)
            {
                partNo = lxConfig.PartNoL;
            }
            else
            {
                partNo = lxConfig.PartNoR;
            }
            var code = $"{partNo}/{lxConfig.SupplyNo}/{batchNo}/{serialNo}/{lxConfig.Version}";
            paras.Add("LBJBH_N1", partNo);
            paras.Add("EWM_N1", code);
            if (Cache.WorkDirection == WorkDirectionEnum.Left)
            {
                if (Cache.IsHopStaion)
                    paras.Add("JQFX_N1", "RL");
                else if (Cache.IsQopStaion)
                    paras.Add("JQFX_N1", "FL");
            }
            else
            {
                if (Cache.IsHopStaion)
                    paras.Add("JQFX_N1", "RR");
                else if (Cache.IsQopStaion)
                    paras.Add("JQFX_N1", "FR");
            }
            LXRecord lXRecord = new LXRecord();
            lXRecord.Code = code;
            lXRecord.SerialNumber = index;
            lXRecord.BatchNo = lxConfig.BatchNo;
            lXRecord.PartNoL = lxConfig.PartNoL;
            lXRecord.PartNoR = lxConfig.PartNoR;
            lXRecord.SerialNo = lxConfig.SerialNo;
            lXRecord.SplitCode = lxConfig.SplitCode;
            lXRecord.SupplyNo = lxConfig.SupplyNo;
            lXRecord.TemplatePath = lxConfig.TemplatePath;
            lXRecord.TemplatePathR = lxConfig.TemplatePathR;
            lXRecord.Version = lxConfig.Version;
            DBContext.DbConfig.DbContext.Insert<LXRecord>(lXRecord).ExecuteIdentity();
            return (paras, code);
        }


        public static string GetLableBarCode(int index)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var lxConfig = config.LxqcConfig;
            //零部件序列号 固定
            var serialNo = GetPartSerialNo(index);
            //生产批次
            var batchNo = GetProductBatch();
            var partNo = string.Empty;
            if (Cache.WorkDirection == WorkDirectionEnum.Left)
            {
                partNo = lxConfig.PartNoL;
            }
            else
            {
                partNo = lxConfig.PartNoR;
            }
            var code = $"{partNo}/{lxConfig.SupplyNo}/{batchNo}/{serialNo}/{lxConfig.Version}";
            return code;
        }

        /// <summary>
        /// 生产批次
        /// </summary>
        /// <returns></returns>
        private static string GetProductBatch()
        {
            var year = DateTime.Now.Year.ToString().Substring(2);
            var mouth = DateTime.Now.Month.ToString("X");
            var day = DateTime.Now.Date.ToString("dd");
            return $"{year}{mouth}{day}";
        }

        /// <summary>
        /// 生产批次
        /// </summary>
        /// <returns></returns>
        private static string GetPartSerialNo(int index)
        {
            var temp = GetProductBatch();
            var rlt = index.ToString("50000");
            return $"{temp}{rlt}";
        }
    }
}